package com.kiln.juxingqs.ui

import android.content.Intent
import androidx.lifecycle.Observer
import client.global.commlib.toast.toast
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.TradeGoodsDetail
import com.kiln.juxingqs.databinding.ActivityZrxyNewBinding
import com.kiln.juxingqs.dialog.JyInputNumDialog
import com.kiln.juxingqs.dialog.PagingFixedBathDddDialog
import com.kiln.juxingqs.receiver.ZrGDListBroadcast
import com.kiln.juxingqs.ui.vm.ZrxyDddViewModel

class ZrXyNewActivity : BaseMiddleActivity<ZrxyDddViewModel, ActivityZrxyNewBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_zrxy_new
    }

    var typeName: String? = ""
    var serverName: String? = ""

    var goods_id: String? = ""
    var goods_name: String? = ""
    var serverType: Int? = 0 //1.服务中心 2.供应商
    var ddd_type: Int? = 0   //1.预支点对点

    var deal_num: String? = ""
    var r_borrow_num: String? = ""
    var f_price: String? = ""
    var g_price: String? = ""
    var titleName: String? = ""
    var goodsPrice: String? = ""
    var user_dict: TradeGoodsDetail.UserDict? = null

    override fun initView() {
        binding?.apply {
            event = this@ZrXyNewActivity
            vm = viewModel

            typeName = intent.getStringExtra("ZrXyType")
            serverType = intent.getIntExtra("serverType", 0)
            ddd_type = intent.getIntExtra("ddd_type", 0)
            serverName = intent.getStringExtra("serverName")
            goods_id = intent.getStringExtra("goods_id")
            goods_name = intent.getStringExtra("goods_name")
            deal_num = intent.getStringExtra("deal_num")
            r_borrow_num = intent.getStringExtra("r_borrow_num")
            f_price = intent.getStringExtra("f_price")
            g_price = intent.getStringExtra("g_price")
            titleName = intent.getStringExtra("titleName")
            goodsPrice = intent.getStringExtra("goods_price")

            user_dict = intent.getParcelableExtra("user_dict")

            viewModel.guaMaiWho.value = typeName
            viewModel.serverType = serverType
            viewModel.ddd_type = ddd_type

            viewModel.serverName.value = serverName
            viewModel.goodsName.value = goods_name
            viewModel.goodsPrice.value = goodsPrice
            viewModel.goodsId.value = goods_id
            viewModel.f_price.value = f_price
            viewModel.g_price.value = g_price

//            viewModel.transfer_price2.value= etJyprice.text.toString()

//            binding.isShowZr = //!titleName.equals("点对点交易") || !titleName.equals("预支点对点")

            binding.isShowZr = when (titleName) {
                "点对点交易" -> true
                "预支点对点" -> true
                "冻结点对点" -> true
                else -> false
            }

            //冻结点对点 2
            if (ddd_type == 1) {
                binding.dealTitle = "可预支数量:" + r_borrow_num
            } else {

                if (ddd_type == 2) {
                    user_dict?.let {
                        binding.dealTitle = "可交易数量:" + it.s_release_num_dj
                    }
                } else {
                    binding.dealTitle = "可交易数量:" + deal_num
                }

            }

        }

    }

    override fun observerUI() {
        viewModel.call.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("操作成功")
                    if (serverType == 0) {
                        startActivity(
                            Intent(
                                this@ZrXyNewActivity,
                                XyzrNewActivity::class.java
                            ).apply {
                                putExtra("index", 2)
                            })
                    }
                    //刷新挂单数列
                    sendBroadcast(Intent(ZrGDListBroadcast.REFREASH_GUADAN_LIST))

                    finish()
                } else {
                    toast(it.message, it.code)
                }
            }
        })

        viewModel.otherAccount.observe(this, {
            viewModel.otherName.value = ""
        })
        viewModel.queryRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    viewModel.otherName.value = it.data?.real_name
                }
            }
        })
    }

    fun selectNum() {
        sendBroadcast(Intent(ZrGDListBroadcast.REFREASH_GUADAN_LIST))

//        PagingFixedBathDddDialog(this@ZrXyNewActivity, goods_id).apply {
//            vm = viewModel
//            ok = {
//                viewModel.selectDddBath.value = it
//            }
//        }.show()

        JyInputNumDialog(this@ZrXyNewActivity).apply {
            ok = { num ->
                viewModel.sNum.value = "${num}"
                dismiss()
            }
        }.show()
    }

    override fun initViewModel(): ZrxyDddViewModel = createViewModel()

    fun lsjl() {
        startActivity(Intent(this, XyzrNewActivity::class.java).apply {
            putExtra("goods_id", goods_id)//新goods_id
        })
    }
}